'use strict';

var gulp = require('gulp');
var path = require('path');
var imagemin = require('gulp-imagemin'), //图片压缩
    pngcrush = require('imagemin-pngcrush'),
    minifycss = require('gulp-minify-css'), //css压缩
    jshint = require('gulp-jshint'), //js检测
    uglify = require('gulp-uglify'); //js压缩
//    concat = require('gulp-concat'), //文件合并
//    rename = require('gulp-rename'), //文件更名
//    notify = require('gulp-notify'); //提示信息

//gulp入口

//覆盖原文件
var DEV_DIR = path.join(process.cwd(), '/frontEnd');
var PRO_DIR = path.join(process.cwd(), '/frontEnd');

// 压缩图片
/*gulp.task('pubimg', function() {
    return gulp.src(path.join(DEV_DIR, '/images/**'))
        .pipe(imagemin({
            progressive: true,
            svgoPlugins: [{
                removeViewBox: false
            }],
            use: [pngcrush()]
        }))
        .pipe(gulp.dest(path.join(PRO_DIR, '/images/')));
});*/

// 压缩css
gulp.task('pubcss', function() {
    return gulp.src(path.join(DEV_DIR, '/css_dev/**/*.css'))
        //.pipe(concat('main.css')) //合并为main.css
        //.pipe(rename({ suffix: '.min' })) //重命名为main.min.css
        .pipe(minifycss()) //压缩
        .pipe(gulp.dest(path.join(PRO_DIR, '/css/')));
});


// 压缩js文件
gulp.task('pubjs', function() {
    return gulp.src(path.join(DEV_DIR, '/js_dev/**/*.js'))
        .pipe(uglify({ mangle: true })) //压缩换行和空格,压缩变量命名
        .pipe(gulp.dest(path.join(PRO_DIR, '/js/')));
});

var scripts = [
    'js_dev/**/*.js'
];
//检查js
gulp.task('lint', function() {
    return gulp.src(scripts)
        .pipe(jshint())
        .pipe(jshint.reporter('jshint-stylish'));
});

gulp.task('default', function() {
    gulp.run('pubcss', 'pubjs',
        'lint');
});
